//
//  MTCertificationUploadCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTCertificationUploadCell: BaseMTInfoVerificationCell {
    
    var frontAsset: MTAsset? {
        didSet {
            requestPhoto(with: frontAsset, isFront: true)
        }
    }
    
    var backAsset: MTAsset? {
        didSet {
            requestPhoto(with: backAsset, isFront: false)
        }
    }
    
    /// 正面照示例图
    lazy var exFrontImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "ex-front")
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    /// 上传正面
    lazy var uploadFrontImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "upload-front")
        imgView.contentMode = .scaleAspectFit
        imgView.isUserInteractionEnabled = true
        let t = UITapGestureRecognizer(target: self, action: #selector(certificationUploadCell(didActiveGestureRecognizerFront:)))
        imgView.addGestureRecognizer(t)
        return imgView
    }()
    
    /// 反面照示例图
    lazy var exBackImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "ex-back")
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    /// 上传反面
    lazy var uploadBackImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "upload-back")
        imgView.contentMode = .scaleAspectFit
        imgView.isUserInteractionEnabled = true
        let t = UITapGestureRecognizer(target: self, action: #selector(certificationUploadCell(didActiveGestureRecognizerBack:)))
        imgView.addGestureRecognizer(t)
        return imgView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

// MARK: ------------------ 初始化 ----------------------
extension MTCertificationUploadCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.textColor = UIColor(hex: 0x1d1a1a)
        titleLabel.font = PingFangSCSemiboldFont(14)
        underline.backgroundColor = UIColor(hex: 0xffffff)
    }
    
    private func initializeUI() {
        contentView.addSubview(exFrontImgView)
        contentView.addSubview(exBackImgView)
        contentView.addSubview(uploadFrontImgView)
        contentView.addSubview(uploadBackImgView)
        titleLabel.textColor = UIColor(hex: 0x1d1a1a)
        titleLabel.font = PingFangSCSemiboldFont(14)
        underline.isHidden = true
        
        titleLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(contentView).offset(10)
            make.left.equalTo(contentView).offset(16)
        }
        
        exFrontImgView.snp.makeConstraints { (make) in
            make.left.equalTo(25)
            make.top.equalTo(titleLabel.snp.bottom).offset(30)
            make.width.equalTo(150)
            make.height.equalTo(100)
        }
        
        uploadFrontImgView.snp.makeConstraints { (make) in
            make.top.equalTo(exFrontImgView)
            make.left.equalTo(exFrontImgView.snp.right).offset(25)
            make.width.equalTo(exFrontImgView)
            make.height.equalTo(exFrontImgView)
        }
        
        exBackImgView.snp.makeConstraints { (make) in
            make.left.equalTo(exFrontImgView)
            make.top.equalTo(exFrontImgView.snp.bottom).offset(15)
            make.width.equalTo(exFrontImgView)
            make.height.equalTo(exFrontImgView)
        }
        
        uploadBackImgView.snp.makeConstraints { (make) in
            make.top.equalTo(exBackImgView)
            make.left.equalTo(exBackImgView.snp.right).offset(25)
            make.width.equalTo(exFrontImgView)
            make.height.equalTo(exFrontImgView)
        }
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension MTCertificationUploadCell {
    
    private func certificationUploadCell(didActiveGestureRecognizerFront gestureRecognizer: UITapGestureRecognizer) {
        NotificationCenter.default.post(name: GlobalNotificationName.idFront.name, object: nil)
    }
    
    private func certificationUploadCell(didActiveGestureRecognizerBack gestureRecognizer: UITapGestureRecognizer) {
        NotificationCenter.default.post(name: GlobalNotificationName.idBack.name, object: nil)
    }
    
}

// MARK: ------------------ 图片请求 ----------------------
extension MTCertificationUploadCell {
    
    private func requestPhoto(with asset: MTAsset?, isFront: Bool) {
        guard let asset = asset else { return }
        MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth / 4, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
            guard let weakself = self else { return }
            /// 设置图片
            if isFront {
                weakself.uploadFrontImgView.image = image
            } else {
                weakself.uploadBackImgView.image = image
            }
        }
    }
}
